<?php
namespace app\bis\controller;
use think\Controller;
class Deal extends  Base
{
    /**
     * @return mixed 商户中心的 deal列表页面
     */
    private $obj;
    public function _initialize()
    {
        $this->obj = model('Deal');
    }
    public function index(){
        $bisId = $this->getLoginUser()->bis_id;
        $Deals = $this->obj->getAllDealsByBisid($bisId);
        return $this->fetch('', [
            'Deals' => $Deals,
        ]);

    }


    //团购列表详情
    public function detail(){
        $bisId = $this->getLoginUser()->bis_id;
        $id=input('get.id');


        //获取一级城市的数据
        $citys = model('City')->getNormalCitysByParentId();
        //获取一级栏目的数据
        $categorys = model('Category')->getNormalCategoryByParentId();
        // 获取团购详情数据
        $DealData = model('Deal')-> getDealdataByid($bisId,$id);


        return $this->fetch('',[
            'citys' => $citys,
            'categorys' => $categorys,
            'dealdata' => $DealData,

        ]);

    }

    public function  add() {
        $bisId = $this->getLoginUser()->bis_id;
        if(request()->isPost()) {
            // 走插入逻辑
            $data = input('post.');
            $location = model('BisLocation')->get($data['location_ids'][0]);

            //图片处理
            $info0 = "";
            $logo = request()->file('logo');
            if ($logo) {
                $info0 = $logo->rule("uniqid")->validate(["size" => 3000000, "ext" => "jpg,png,gif"])->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'Simages' . DS . 'spimages');
            }
            $data['image'] =$info0->getFilename();



            $deals = [
                'bis_id' => $bisId,
                'name' => $data['name'],
                'image' => $data['image'],
                'category_id' => $data['category_id'],
                'se_category_id' => empty($data['se_category_id']) ? '' : implode(',', $data['se_category_id']),
                'city_id' => $data['city_id'],
                'location_ids' => empty($data['location_ids']) ? '' : implode(',', $data['location_ids']),
                'start_time' => strtotime($data['start_time']),
                'end_time' => strtotime($data['end_time']),
                'total_count' => $data['total_count'],
                'origin_price' => $data['origin_price'],
                'current_price' => $data['current_price'],
                'coupons_begin_time' => strtotime($data['coupons_begin_time']),
                'coupons_end_time' => strtotime($data['coupons_end_time']),
                'notes' => $data['notes'],
                'status' => 1,
                'description' => $data['description'],
                'bis_account_id' => $this->getLoginUser()->id,
                'xpoint' => $location->xpoint,
                'ypoint' => $location->ypoint,


            ];

            $id = model('Deal')->add($deals);
            if($id) {
                $this->success('添加成功', url('deal/index'));
            }else {
                $this->error('添加失败');
            }

        }else {
            //获取一级城市的数据
            $citys = model('City')->getNormalCitysByParentId();
            //获取一级栏目的数据
            $categorys = model('Category')->getNormalCategoryByParentId();
            return $this->fetch('', [
                'citys' => $citys,
                'categorys' => $categorys,
                'bislocations' => model('BisLocation')->getNormalLocationByBisId($bisId),
            ]);
        }
    }

}
